package com.wrk._3_方法._02_习题;

import java.util.Random;

public class _008_红包抽奖 {
    public static void main(String[] args) {
        //1、定义抽奖红包为数组
        int[] redPacket = {2,588,888,1000,10000};
        //定义抽奖结果为新数组,接收结果
        int [] raffleResults =raffleArray(redPacket,redPacket.length);
        //遍历抽奖结果
        System.out.print("抽奖结果为：");
        for (int i = 0; i < raffleResults.length; i++) {
            System.out.print(raffleResults[i]+" ");
        }
    }

    /**抽奖方法*/
    public static int [] raffleArray(int[] redPacket,int length){
        //定义抽奖结果为新数组,
        int [] raffle = new int[length];
        int i = 0;
        //遍历抽奖结果数组
        while (i < raffle.length){
            boolean fale = true;
            //2、随机生成0-4之间随机索引
            int readom =  new Random().nextInt(redPacket.length);
            //遍历红包数组，判断是否存在
            for (int j = 0; j < redPacket.length; j++) {
                if (redPacket[readom] == raffle[j]){
                    fale =false;
                    break;
                }
            }
            //抽取元素不重复，赋值，并抽取下次，否则重新抽取
            if (fale){
                raffle[i] = redPacket[readom];
                i++;
            }
        }
        return raffle;
    }
}
